//detail模块下的vuex
import { GetGoodsInfo, PostAddOrUpdateShopCar } from '@/api'


const state = { //存储数据
    goodsInfo: {},
}

const mutations = { //唯一修改state中的数据，方法名大写
    GOODS_INFO(state, goodsInfo) {
        state.goodsInfo = goodsInfo;
    },
}

const actions = { //方法的中转，然后调用mutations中的方法
    async getGoodsInfo({ commit }, skuId) {
        let result = await GetGoodsInfo(skuId);
        if (result.code == 200) {
            commit('GOODS_INFO', result.data)
        }
    },
    async postAddOrUpdateShopCar({ commit }, { skuId, skuNum }) {
        let result = await PostAddOrUpdateShopCar(skuId, skuNum);
        if (result.code == 200) {
            return result.ok;
        } else {
            return Promise.reject("Failed")
        }
    }

}

const getters = { //相当于计算属性
    categoryView(state) {
        return state.goodsInfo.categoryView || {};
    },
    skuInfo(state) {
        return state.goodsInfo.skuInfo || {};
    },
    spuSaleAttrList(state) {
        return state.goodsInfo.spuSaleAttrList || [];
    },
}

export default { state, mutations, actions, getters }